package com.huanbao.oil.app.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huanbao.oil.app.R;
import com.huanbao.oil.app.adapter.StatementAdapter;
import com.huanbao.oil.app.application.MessageEvent;
import com.huanbao.oil.app.commons.Arithmetic4Double;
import com.huanbao.oil.app.commons.JsonUtil;
import com.huanbao.oil.app.commons.UtilsLog;
import com.huanbao.oil.app.model.MainModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class StatementFragment extends Fragment {
    public static final String BUNDLE_TITLE = "title";
    @BindView(R.id.item_statement_21)
    TextView itemStatement21;
    @BindView(R.id.item_statement_23)
    TextView itemStatement23;
    @BindView(R.id.item_statement_24)
    TextView itemStatement24;
    @BindView(R.id.item_statement_25)
    TextView itemStatement25;
    @BindView(R.id.item_statement_26)
    TextView itemStatement26;
    @BindView(R.id.item_banre_shuchu_zhishi)
    TextView itemBanreShuchuZhishi;
    @BindView(R.id.item_statement_28)
    TextView itemStatement28;
    @BindView(R.id.item_beiyong)
    TextView itemBeiyong;
    @BindView(R.id.item_1_hao_lengxiang_zhirezhishi)
    TextView item1HaoLengxiangZhirezhishi;
    @BindView(R.id.item_1_hao_lengxiang_zhileng_zhishi)
    TextView item1HaoLengxiangZhilengZhishi;
    @BindView(R.id.item_statement_27)
    TextView itemStatement27;
    @BindView(R.id.item_yasuoji_2_1_guzhang_zhishi)
    TextView itemYasuoji21GuzhangZhishi;
    @BindView(R.id.item_yasuoji_2_2_guzhang_zhishi)
    TextView itemYasuoji22GuzhangZhishi;
    @BindView(R.id.item_zhenkongbeng_guzhang_zhishi)
    TextView itemZhenkongbengGuzhangZhishi;
    @BindView(R.id.item_youbeng_guzhang_zhishi)
    TextView itemYoubengGuzhangZhishi;
    @BindView(R.id.item_fengji_guzhang_zhishi)
    TextView itemFengjiGuzhangZhishi;
    @BindView(R.id.item_chengxu_zijian_baojing_zhishi)
    TextView itemChengxuZijianBaojingZhishi;
    @BindView(R.id.item_2_1_yasuoji_guoya_baojing)
    TextView item21YasuojiGuoyaBaojing;
    @BindView(R.id.item_2_2_yasuoji_guoya_baojing)
    TextView item22YasuojiGuoyaBaojing;
    @BindView(R.id.item_xifufa_kaidaowei)
    TextView itemXifufaKaidaowei;
    @BindView(R.id.item_xifufa_guandaowei)
    TextView itemXifufaGuandaowei;
    @BindView(R.id.item_jiting_baojing_zhishi)
    TextView itemJitingBaojingZhishi;
    Unbinder unbinder;
    @BindView(R.id.item_jixifa_kaidaowei)
    TextView itemJixifaKaidaowei;
    @BindView(R.id.item_jixifa_guandaowei)
    TextView itemJixifaGuandaowei;
    @BindView(R.id.item_shou_zidong_zhishi)
    TextView itemShouZidongZhishi;

    private String mTitle = "DefaultValue";
    private MainActivity mainActivity;

    private StatementAdapter statementAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mainActivity = (MainActivity) getActivity();
        EventBus.getDefault().register(this);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mTitle = arguments.getString(BUNDLE_TITLE);
        }
        View v = inflater.inflate(R.layout.statement_fragment2, null);

        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
//        mainActivity.setSocketData(3);
//        statementAdapter = new StatementAdapter(mainActivity, null);
//        statementFragmentListview.setAdapter(statementAdapter);
        mainActivity.setSocketData3();
//        mainActivity.setSocketData(3);
    }

    public static StatementFragment newInstance(String title) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_TITLE, title);
        StatementFragment fragment = new StatementFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }


    List<MainModel> modelList = new ArrayList<>();

    @Subscribe(threadMode = ThreadMode.ASYNC, sticky = true)
    public void onMessageEvent(MessageEvent event) {
        if (mainActivity.TCP_MODE == 3) {
            if (!TextUtils.isEmpty(event.getMessage()) && !"{}".equals(event.getMessage())
                    && !"{[]}".equals(event.getMessage())) {
                try {
                    JSONObject jsonObject = new JSONObject(event.getMessage());
                    int cmd_type = 0;
                    if (jsonObject.has("cmd_type")) {
                        cmd_type = jsonObject.getInt("cmd_type");
                        UtilsLog.e("StatementFragmentcmd_type==" + cmd_type);
                    }
                    if (cmd_type == 4) {
                        if (jsonObject.getString("pnts") != "[]" && !TextUtils.isEmpty(jsonObject.getString("pnts"))) {
                            modelList = JsonUtil.stringToArray(jsonObject.getString("pnts"), MainModel[].class);
                            if (modelList != null && modelList.size() > 0) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Message msg = new Message();
                                        msg.obj = modelList;
                                        msg.arg1 = 1;
                                        handler_statement.sendMessage(msg);
                                    }
                                }).start();
                            } else {
                                UtilsLog.e("StatementFragment零条数据==");
                            }
                        }
                    } else {
                        UtilsLog.e("StatementFragment cmd_type!==2");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    UtilsLog.e("StatementFragment进入解析==" + e.getMessage());
                }
            }
        }
    }

    private Handler handler_statement = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.obj != null) {
                List<MainModel> lList = (List<MainModel>) msg.obj;
                List<String> stringList = new ArrayList<>();
                for (int i = 0; i < lList.size(); i++) {
                    HashMap<String, String> map = mainActivity.getDataHashMap();
                    if (!TextUtils.isEmpty(map.get(String.valueOf(lList.get(i).getPnt_id())))) {
                        map.put(String.valueOf(lList.get(i).getPnt_id()), String.valueOf(lList.get(i).getValue()));
                    } else {
                        UtilsLog.e("StatementFragmentHashMap==数据为空");
                    }
                }
//
                //压缩机2-1运行指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("26")) > 0) {
                    itemStatement21.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("26")));
                    itemStatement21.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemStatement21.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("26")));
                    itemStatement21.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //压缩机2-1故障指示
                UtilsLog.e("压缩机2-1故障指示==" + mainActivity.getDataHashMap().get("6"));
                if (Double.parseDouble(mainActivity.getDataHashMap().get("6")) != 0) {
                    UtilsLog.e("11111111111111" );
                    itemYasuoji21GuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("6")));
//                    itemYasuoji21GuzhangZhishi.setText("aaaaaaaaaaaa");
                    itemYasuoji21GuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    UtilsLog.e("22222222222222" );
                    itemYasuoji21GuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("6")));
//                    itemYasuoji21GuzhangZhishi.setText("bbbbbbbbbbbbbbb");
                    itemYasuoji21GuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //压缩机2-1报警指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("12")) > 0) {
                    item21YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("12")));
//                    item21YasuojiGuoyaBaojing.setText("bbbbbbbbbbbbbbb");
                    item21YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
//                    item21YasuojiGuoyaBaojing.setText("ccccccc");
                    item21YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("12")));
                    item21YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //压缩机2-2运行指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("27")) > 0) {
                    itemStatement23.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("27")));
                    itemStatement23.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemStatement23.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("27")));
                    itemStatement23.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //压缩机2-2故障指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("7")) > 0) {
                    itemYasuoji22GuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("7")));
                    itemYasuoji22GuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemYasuoji22GuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("7")));
                    itemYasuoji22GuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //压缩机2-2报警指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("13")) > 0) {
                    item22YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("13")));
                    item22YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    item22YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("13")));
                    item22YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //真空泵运行指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("28")) > 0) {
                    itemStatement24.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("28")));
                    itemStatement24.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemStatement24.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("28")));
                    itemStatement24.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //真空泵故障指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("8")) > 0) {
                    itemZhenkongbengGuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("8")));
                    itemZhenkongbengGuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemZhenkongbengGuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("8")));
                    itemZhenkongbengGuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //急停报警指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("19")) > 0) {
                    itemJitingBaojingZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("19")));
                    itemJitingBaojingZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemJitingBaojingZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("19")));
                    itemJitingBaojingZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
//                stringList.add(mainActivity.getDataHashMap().get("105"));
//                stringList.add(mainActivity.getDataHashMap().get("12"));
                //油泵运行指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("29")) > 0) {
                    itemStatement25.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("29")));
                    itemStatement25.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemStatement25.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("29")));
                    itemStatement25.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //油泵故障指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("9")) > 0) {
                    itemYoubengGuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("9")));
                    itemYoubengGuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemYoubengGuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("9")));
                    itemYoubengGuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
//                stringList.add(mainActivity.getDataHashMap().get("106"));
//                stringList.add(mainActivity.getDataHashMap().get("13"));
                //风机运行指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("30")) > 0) {
                    itemStatement26.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("30")));
                    itemStatement26.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemStatement26.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("30")));
                    itemStatement26.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //风机故障指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("10")) > 0) {
                    itemFengjiGuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("10")));
                    itemFengjiGuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemFengjiGuzhangZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("10")));
                    itemFengjiGuzhangZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
//                stringList.add(mainActivity.getDataHashMap().get("108"));
//                stringList.add(mainActivity.getDataHashMap().get("14"));
                //吸附阀运行指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("36")) > 0) {
                    itemStatement27.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("36")));
                    itemStatement27.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemStatement27.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("36")));
                    itemStatement27.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //程序自检报警指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("11")) > 0) {
                    itemChengxuZijianBaojingZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("11")));
                    itemChengxuZijianBaojingZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemChengxuZijianBaojingZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("11")));
                    itemChengxuZijianBaojingZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }

//                stringList.add(mainActivity.getDataHashMap().get("107"));
                if (Double.parseDouble(mainActivity.getDataHashMap().get("32")) > 0) {
                    //解析阀运行指示
                    itemStatement28.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("32")));
                    itemStatement28.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemStatement28.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("32")));
                    itemStatement28.setBackgroundColor(getResources().getColor(R.color.white));
                }

                //伴热输出指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("31")) > 0) {
                    itemBanreShuchuZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("31")));
                    itemBanreShuchuZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemBanreShuchuZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("31")));
                    itemBanreShuchuZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }

                //1号冷箱制热指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("34")) > 0) {
                    item1HaoLengxiangZhirezhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("34")));
                    item1HaoLengxiangZhirezhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    item1HaoLengxiangZhirezhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("34")));
                    item1HaoLengxiangZhirezhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //1号冷箱制冷指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("35")) > 0) {
                    item1HaoLengxiangZhilengZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("35")));
                    item1HaoLengxiangZhilengZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    item1HaoLengxiangZhilengZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("35")));
                    item1HaoLengxiangZhilengZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
//                //2-1压缩机过压报警
//                if (Double.parseDouble(mainActivity.getDataHashMap().get("6")) > 0) {
//                    item21YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("6")));
//                    item21YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.red));
//                } else {
//                    item21YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("6")));
//                    item21YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.white));
//                }
//                //2-2压缩机过压报警
//                if (Double.parseDouble(mainActivity.getDataHashMap().get("7")) > 0) {
//                    item22YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("7")));
//                    item22YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.red));
//                } else {
//                    item22YasuojiGuoyaBaojing.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("7")));
//                    item22YasuojiGuoyaBaojing.setBackgroundColor(getResources().getColor(R.color.white));
//                }
                //吸附阀开到位
                if (Double.parseDouble(mainActivity.getDataHashMap().get("14")) > 0) {
                    itemXifufaKaidaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("14")));
                    itemXifufaKaidaowei.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemXifufaKaidaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("14")));
                    itemXifufaKaidaowei.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //吸附阀关到位
                if (Double.parseDouble(mainActivity.getDataHashMap().get("15")) > 0) {
                    itemXifufaGuandaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("15")));
                    itemXifufaGuandaowei.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemXifufaGuandaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("15")));
                    itemXifufaGuandaowei.setBackgroundColor(getResources().getColor(R.color.white));
                } //解析阀开到位
                if (Double.parseDouble(mainActivity.getDataHashMap().get("16")) > 0) {
                    itemJixifaKaidaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("16")));
                    itemJixifaKaidaowei.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemJixifaKaidaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("16")));
                    itemJixifaKaidaowei.setBackgroundColor(getResources().getColor(R.color.white));
                }
                //解析阀关到位
                if (Double.parseDouble(mainActivity.getDataHashMap().get("17")) > 0) {
                    itemJixifaGuandaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("17")));
                    itemJixifaGuandaowei.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemJixifaGuandaowei.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("17")));
                    itemJixifaGuandaowei.setBackgroundColor(getResources().getColor(R.color.white));
                }

                //手自动指示
                if (Double.parseDouble(mainActivity.getDataHashMap().get("18")) > 0) {
                    itemShouZidongZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("18")));
                    itemShouZidongZhishi.setBackgroundColor(getResources().getColor(R.color.red));
                } else {
                    itemShouZidongZhishi.setText(Arithmetic4Double.keepZero(mainActivity.getDataHashMap().get("18")));
                    itemShouZidongZhishi.setBackgroundColor(getResources().getColor(R.color.white));
                }
//                if (stringList != null && stringList.size() > 0) {
//                    statementAdapter = new StatementAdapter(mainActivity, stringList);
//                    statementFragmentListview.setAdapter(statementAdapter);
//                    statementFragmentListview.setVisibility(View.VISIBLE);
//                } else {
//                    UtilsLog.e("StatementFragmentHashMap==stringList===数据为空");
//                }
            } else {
//                statementFragmentListview.setVisibility(View.GONE);
            }
        }
    };

}
